package com.zhiyou.bbs_server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhiyou.bbs01.MessageDao;
import com.zhiyou.bbs01.User;

/**
 * Servlet implementation class Comment_servlet
 */
public class Comment_servlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("user");
		if(u==null){
			response.sendRedirect("/bbs/page/login.jsp");
		}else{
			Integer tid = Integer.parseInt(request.getParameter("tid"));
			Integer uid = u.getUid();
			String mcontent = request.getParameter("mcontent");
			MessageDao md = new MessageDao();
			md.insertMessage(tid, uid, mcontent);
			response.sendRedirect("/bbs/page/index.jsp");
		}
	}

}
